/**
 * @author Praveen Krupakaran (A0092625)
 */

package sg.edu.nus.iss.team4ft.ui;
import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.DebugGraphics;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.SwingUtilities;
import sg.edu.nus.iss.team4ft.service.User;
import sg.edu.nus.iss.team4ft.util.AppConstant;
import sg.edu.nus.iss.team4ft.util.LoginIOService;


	


public class OneHashUI extends javax.swing.JFrame {

			/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


			{
				//Set Look & Feel
				try {
					javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
				} catch(Exception e) {
					e.printStackTrace();
				}
			}

			private JButton logoutButton;
			private JPanel logoutpanel;
			private JLabel jLabel1;
			private JButton cancelButton;
			private JButton loginButton;
			private JPasswordField passwordText;
			private JTextField loginText;
			private JLabel passwordLabel;
			private JLabel userNameLabel;
			private JLabel memberLoginLabel;
			private JPanel jPanel1;
			private JPanel centerpanel;
			private JPanel mainpanel;
			private JLabel imageLabel;
			private JPanel imagePanel;
			private ApplicationJPanel app;
			private JLabel usernameLabel;

			/**
			* Auto-generated main method to display this JFrame
			*/
			public static void main(String[] args) {
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						OneHashUI inst;
						
						
						try {
							inst = new OneHashUI();
							inst.setLocationRelativeTo(null);
							inst.setVisible(true);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (NullPointerException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (ParseException e) {
							
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (ClassNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						
					}
				});
			}
			
			public OneHashUI() throws IOException, NullPointerException, ParseException, ClassNotFoundException {
				super();
				initGUI();
				
			}
			
			
			//initialize main GUI 
			private void initGUI() {
				try {
					
					BorderLayout thisLayout = new BorderLayout();
					getContentPane().setLayout(thisLayout);
					setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
					Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
				    this.setSize(screenSize.width,screenSize.height);
					this.setTitle("One# Application");
					
					getContentPane().setBackground(new java.awt.Color(210,225,247));
					getContentPane().setForeground(new java.awt.Color(0,64,128));
					{
						
						mainpanel = new JPanel();
						BorderLayout mainpanelLayout = new BorderLayout();
						mainpanel.setLayout(mainpanelLayout);
						new AnchorLayout();
						getContentPane().add(mainpanel, BorderLayout.CENTER);
						mainpanel.setLayout(mainpanelLayout);
						mainpanel.setPreferredSize(new java.awt.Dimension(890, 476));
						mainpanel.setBackground(new java.awt.Color(183,206,242));
						{
							centerpanel = new JPanel();
							AnchorLayout centerpanelLayout = new AnchorLayout();
							mainpanel.add(centerpanel, BorderLayout.CENTER);
							centerpanel.setLayout(centerpanelLayout);
							centerpanel.setBackground(new java.awt.Color(206,208,251));
							centerpanel.setPreferredSize(new java.awt.Dimension(890, 538));
							
							{
								jPanel1 = new JPanel();
								centerpanel.add(jPanel1, new AnchorConstraint(519, 839, 975, 179, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
								AnchorLayout jPanel1Layout = new AnchorLayout();
								jPanel1.setLayout(jPanel1Layout);
								new AnchorLayout();
								jPanel1.setLayout(jPanel1Layout);
								jPanel1.setBackground(new java.awt.Color(177,180,248));
								jPanel1.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
								jPanel1.setPreferredSize(new java.awt.Dimension(588, 217));
								{
									cancelButton = new JButton();
									jPanel1.add(cancelButton, new AnchorConstraint(716, 609, 813, 439, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
									cancelButton.setText("Cancel");
									cancelButton.setLayout(null);
									cancelButton.setPreferredSize(new java.awt.Dimension(100, 21));
									cancelButton.addActionListener(new ActionListener() {
										public void actionPerformed(ActionEvent evt) {
											cancelButtonactionPerformed(evt);
										}
									});
								}
								{
									loginButton = new JButton();
									jPanel1.add(loginButton, new AnchorConstraint(716, 412, 813, 237, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
									loginButton.setText("Login");
									loginButton.setLayout(null);
									loginButton.setPreferredSize(new java.awt.Dimension(103, 21));
									
									loginButton.addActionListener(new ActionListener() {
										public void actionPerformed(ActionEvent evt) {
											try {
												loginButtonactionPerformed(evt);
											} catch (NullPointerException e) {
												// TODO Auto-generated catch block
												e.printStackTrace();
											} catch (IOException e) {
												// TODO Auto-generated catch block
												e.printStackTrace();
											}
										}
									});
								}
								{
									passwordText = new JPasswordField();
									jPanel1.add(passwordText, new AnchorConstraint(495, 609, 581, 394, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
									passwordText.setPreferredSize(new java.awt.Dimension(133, 20));
								}
								{
									loginText = new JTextField();
									jPanel1.add(loginText, new AnchorConstraint(329, 609, 412, 394, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
									loginText.setPreferredSize(new java.awt.Dimension(126, 18));
								}
								{
									passwordLabel = new JLabel();
									jPanel1.add(passwordLabel, new AnchorConstraint(517, 353, 581, 191, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
									passwordLabel.setText("Password");
									passwordLabel.setLayout(null);
									passwordLabel.setPreferredSize(new java.awt.Dimension(100, 15));
								}
								{
									userNameLabel = new JLabel();
									jPanel1.add(userNameLabel, new AnchorConstraint(332, 350, 414, 191, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
									userNameLabel.setText("UserName");
									userNameLabel.setLayout(null);
									userNameLabel.setPreferredSize(new java.awt.Dimension(98, 19));
								}
								{
									memberLoginLabel = new JLabel();
									jPanel1.add(memberLoginLabel, new AnchorConstraint(122, 496, 214, 181, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
									memberLoginLabel.setText("Members Login");
									memberLoginLabel.setLayout(null);
									memberLoginLabel.setPreferredSize(new java.awt.Dimension(185, 20));
									memberLoginLabel.setFont(new java.awt.Font("Arial Narrow",1,20));
								}
							}
							{
								jLabel1 = new JLabel();
								AnchorLayout jLabel1Layout1 = new AnchorLayout();
								new AnchorLayout();
								centerpanel.add(jLabel1, new AnchorConstraint(47, 812, 494, 216, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
								jLabel1.setLayout(jLabel1Layout1);
								jLabel1.setIcon(new ImageIcon(getClass().getClassLoader().getResource("Images/consumer-affairs_02_temp-1309069683-4e06d173-620x348.jpg")));
								jLabel1.setPreferredSize(new java.awt.Dimension(531, 213));
							}
							{
								logoutpanel = new JPanel();
								AnchorLayout logoutpanelLayout = new AnchorLayout();
								centerpanel.add(logoutpanel, new AnchorConstraint(-93, 997, -5, 771, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
								logoutpanel.setLayout(logoutpanelLayout);
								logoutpanel.setPreferredSize(new java.awt.Dimension(201, 42));
							}
						}
					}

					{
						imagePanel = new JPanel();
						AnchorLayout imagePanelLayout = new AnchorLayout();
						imagePanel.setLayout(imagePanelLayout);
						getContentPane().add(imagePanel, BorderLayout.NORTH);
						imagePanel.setPreferredSize(new java.awt.Dimension(890, 57));
						imagePanel.setBackground(new java.awt.Color(203,205,250));
						{
							logoutButton = new JButton();
							new AnchorLayout();
							imagePanel.add(logoutButton, new AnchorConstraint(410, 994, 930, 897, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
							logoutButton.setText("Logout");
							logoutButton.setLayout(null);
							logoutButton.setPreferredSize(new java.awt.Dimension(87, 26));
							logoutButton.setVisible(false);
							logoutButton.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) {
									try {
										logoutButtonactionPerformed(evt);
									} catch (IOException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									} catch (NullPointerException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									} catch (ParseException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									} catch (ClassNotFoundException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}
							});
						}
						{
							imageLabel = new JLabel();
							AnchorLayout image2labelLayout = new AnchorLayout();
							imageLabel.setLayout(image2labelLayout);
							imagePanel.add(imageLabel, new AnchorConstraint(8, 769, 782, 296, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
							imageLabel.setPreferredSize(new java.awt.Dimension(421, 48));
							imageLabel.setText("ONE # TELEPHONY SERVICES");
							imageLabel.setFont(new java.awt.Font("Segoe UI Symbol",1,28));
							imageLabel.setBackground(new java.awt.Color(0,0,128));
							imageLabel.setForeground(new java.awt.Color(128,0,64));
						}
						{
							usernameLabel = new JLabel();
							imagePanel.add(usernameLabel, new AnchorConstraint(500, 176, 885, 23, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
							usernameLabel.setLayout(null);
							usernameLabel.setPreferredSize(new java.awt.Dimension(137, 22));
							usernameLabel.setFont(new java.awt.Font("Segoe UI",1,14));
							usernameLabel.setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);
						}
					}
					this.pack();
					
				
				} catch (Exception e) {
				    //add your error handling code here
					e.printStackTrace();
				}
			}
			
			
			//action to be performed on clicking login button
			private void loginButtonactionPerformed(ActionEvent evt) throws NullPointerException, IOException {
			
				
				String userName = loginText.getText();
				char[] password = passwordText.getPassword();
				boolean var = validateUser(userName ,password );
				if(var)
				{					
					app = new ApplicationJPanel();
					mainpanel.remove(centerpanel);
					mainpanel.add(app, BorderLayout.CENTER);
					logoutButton.setVisible(true);
					mainpanel.validate();
					this.setExtendedState(Frame.MAXIMIZED_BOTH);
					usernameLabel.setText("Welcome      "  + userName);
				}
				else{
					JOptionPane.showMessageDialog(null, "Please enter correct username or password");
				}
			}

			
			//validate user for authenication
			private boolean validateUser(String userName, char[] password) throws NullPointerException, IOException {
				LoginIOService login = new LoginIOService();
				List<User> userlist = login.getUserLst();
				for (User user: userlist  ){
					String name = user.getUsername();
					if(name.compareTo(userName) == 0){
						String pass = new String(password);
						if(user.getPassword().compareTo(pass) ==  0){
							AppConstant.USER = user;
							System.out.println("Username:"+user.getUsername()+" Role:"+user.getRole());
							return true;
						}
						else
							return false;
					}
					else
						continue;
				}
				return false;
				
				
			}
			
			
			//action to be performed on clicking logout button
			private void logoutButtonactionPerformed(ActionEvent evt) throws IOException, NullPointerException, ParseException, ClassNotFoundException {
				
				this.dispose();
				OneHashUI inst = new OneHashUI();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
				
			}
			
			
			//action to be performed on clicking cancel button
			private void cancelButtonactionPerformed(ActionEvent evt) {
				System.out.println("jButton2.actionPerformed, event="+evt);
				loginText.setText("");
				passwordText.setText("");
			}

	}
